<?php
session_start();

// akaltun mickro 1876988499
$config_file = 'config.php';
if (!file_exists($config_file) || trim(file_get_contents($config_file)) === '') {
    header('Location: login.php');
    exit;
}
include $config_file;
if (!isset($website_name) || empty(trim($website_name))) {
    header('Location: login.php');
    exit;
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <?php include 'head.php'; ?> 
    <title><?php echo htmlspecialchars($website_name); ?>-مۇنبەر</title>
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
    <header>
        <h1><?php echo htmlspecialchars($website_name); ?></h1>
        <!-- 显示你的网站名称 -->
        <a href="login.php" class="button">كىرىش后台</a>
    </header>
<main>
    <div class="article-list">
        <?php
        $articlesDir = 'articles/';
        if ($handle = opendir($articlesDir)) {
            $articles = [];
            while (($file = readdir($handle)) !== false) {
                if (pathinfo($file, PATHINFO_EXTENSION) === 'html' && $file !== "." && $file !== "..") {
                    $articles[] = $file;
                }
            }
            closedir($handle);
            
            usort($articles, function($a, $b) use ($articlesDir) {
                return filemtime($articlesDir.$b) - filemtime($articlesDir.$a);
            });

            foreach ($articles as $file) {
                $articleFile = $articlesDir . $file;
                $content = file_get_contents($articleFile);
                
                preg_match('/<h1>(.*?)<\/h1>/is', $content, $titleMatches);
                preg_match('/<p class="excerpt">(.*?)<\/p>/is', $content, $excerptMatches);
                
                $title = isset($titleMatches[1]) ? htmlspecialchars($titleMatches[1]) : 'yok';
                $excerpt = isset($excerptMatches[1]) ? strip_tags($excerptMatches[1]) : '';

                echo '<a href="makala.php?file=' . urlencode($file) . '" class="article-item">';
                echo '<div>';
                echo '<h2>' . $title . '</h2>';
                if (!empty($excerpt)) {
                    echo '<p class="excerpt">' . htmlspecialchars($excerpt) . '</p>';
                }
                echo '<div class="meta">';
                echo '</br><time style="font-size: 0.6em;  align="center">' . date("Y-m-d H:i", filemtime($articleFile)) . '</time>';
                echo '</div>';
                echo '</div>';
                echo '</a>';
            }
        }
        ?>
    </div>
</main>
    
    <table border="0" cellspacing="0" cellpadding="0" height="100%" width="100%">
        <tr>
            <td align="center" valign="bottom">
                <footer>
                    <p>&copy;akaltun 2018 مىكرو</p>
                </footer>
            </td>
        </tr>
        </table>
      
<script src="assets/js/su.js"></script>
</body>
</html>
